package com.loe.httptest

import androidx.annotation.Keep
import com.loe.http.Link
import com.loe.json_ext.Json
import com.loe.json_ext.JsonArray
import com.loe.json_ext.isEmpty
import com.loe.json_ext.toList
import com.loe.mvp.ext_java.delay
import com.loe.mvp.util.CodeBean
import java.io.Serializable

@Keep
internal class HttpTestBean : Serializable
{
    var id = 0L
    var name = ""
    var url = ""
    var params = ""
    var type = ""
    var isJson = false

    var result = ""

    fun request(onResult: (String) -> Unit)
    {
        val link = Link(SpHttpTest.baseUrl + url)
        link.header(SpHttpTest.tokenKey, SpHttpTest.token)
        link.type(type)

        val params = JsonArray(params).toList<HttpParam>()
        for (param in params)
        {
            if(param.key.isNotBlank())
            {
                link.param(param.key, param.realValue)
            }
        }

        if (isJson) link.isJson()

        link.error(onResult).okString(onResult)

        delay(500)
        {
            link.send()
        }
    }

    fun putResult(s: String)
    {
        val resultJson = Json(s)
        val result = if (resultJson.isEmpty()) s else resultJson.toString(4)
        this.result = result
    }
}